package com.itheima.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.R;
import com.itheima.common.JwtUtils;
import com.itheima.mapper.backend.EmployeeDao;
import com.itheima.pojo.backend.Employee;
import com.itheima.pojo.backend.Paging;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class EmployeeService {
    @Autowired
    private EmployeeDao employeeDao;

    @Autowired
    StringRedisTemplate stringRedisTemplate;


    /**
     * 登入
     *
     * @param
     * @return
     */
    public R<Object> login(Employee employee) {
        String newPwd = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        employee.setPassword(newPwd);
        Employee login = employeeDao.login(employee);
        if (login==null){
            return R.error("用户不存在");
        }
        if (login.getUsername() == null) {
            return R.error("用户不存在");
        }
        if (login.getStatus() == 0) {
            return R.error("该用户已经被禁用");
        }

        if (newPwd.equals(login.getPassword())) {
            Map<String, Object> map = new HashMap<>();
            map.put(login.getUsername(), login.getPassword());
            String token = JwtUtils.geneJsonWebToken(map);
            if (stringRedisTemplate.opsForValue().get(token) == null) {
                stringRedisTemplate.opsForValue().set(token, login.getUsername(), JwtUtils.EXPIRE, TimeUnit.SECONDS);
            }
            login.setToken(token);
            return R.success(login);
        } else {
            return R.error("密码输入不正确请重新输入");
        }
    }


    public R logout(HttpServletRequest request){
        String header = request.getHeader("Authorization");
        if (header!=null){
            stringRedisTemplate.delete(header);
            return R.success("退出登入");
        }
        return R.error("还没有登入");
    }

    /**
     * 添加
     *
     * @param employee
     * @return
     */
    public R add(HttpServletRequest request, Employee employee) {
        List<Employee> exist = employeeDao.isExist(employee);
        if (exist.size()!=0) {
            return R.error(employee.getUsername()+"用户名已经存在");
        }

        //密码加密
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);

        //获取创建者
        String token = request.getHeader("Authorization");
        String s = stringRedisTemplate.opsForValue().get(token);
        employee.setCreateUser(s);
        employee.setUpdateUser(s);


        //获取当前时间
        long l = System.currentTimeMillis();
        employee.setCreateTime(String.valueOf(l));
        employee.setUpdateTime(String.valueOf(l));

        int add = employeeDao.add(employee);
        if (add > 0) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败，请重试");
        }

    }

    /**
     * 分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */

    public R select(Integer page, Integer pageSize, String name) {
        //构建分页拦截器
        PageHelper.startPage(page, pageSize);

        List<Employee> employeeList = employeeDao.select(name);

        PageInfo<Employee> PageInfo = new PageInfo<>(employeeList);

        //返回值对象，这里的P类是根据前端要的数据专门写的类，。属性只有这四条
        Paging p = new Paging();
        //总条数
        p.setTotal(PageInfo.getTotal());
        //每页显示条数
        p.setSize(PageInfo.getSize());
        //当前页码
        p.setCurrent(PageInfo.getPageNum());
        //被展示的数据集
        p.setRecords(employeeList);
        return R.success(p);
    }



    /**
     * 按id查询
     * @param id
     * @return
     */

    public Employee selectByid(Integer id) {
        return   employeeDao.selectByid(id);
    }

    /**
     * 修改
     * @param employee
     * @return
     */
    public R updatemp(HttpServletRequest request,Employee employee){


        long l = System.currentTimeMillis();
        employee.setUpdateTime(String.valueOf(l));

        String token = request.getHeader("Authorization");
        String s = stringRedisTemplate.opsForValue().get(token);
        System.out.println(s);
        employee.setUpdateUser(s);

        int updatemp = employeeDao.updatemp(employee);
        if (updatemp>0){
            List<Employee> exist = employeeDao.isExist(employee);
            if (exist.size()!=0) {
                return R.error(employee.getUsername()+"用户名已经存在");
            }else {
                return R.success("员工信息修改成功");
            }
        }else {
            return R.error("员工信息修改失败");
        }
    }

}

